package {
	import net.flashpunk.graphics.PreRotation;

	/**
	 * @author guido
	 */
	public class Rock extends PhysicsEntity {
		import net.flashpunk.graphics.Image;
		import net.flashpunk.FP;

		[Embed(source = 'assets/rock.png')]
		private const ROCK : Class;
		private var rock : PreRotation;
		private var hFriction : Number = 0.95;

		public function Rock(x : Number, y : Number) {
			this.x = x;
			this.y = y;
			rock = new PreRotation(ROCK);
			graphic = rock;
			setHitbox(27, 26);
			type = "bullet";
		}

		override public function update() : void {
			if (onTheGround) {
				xSpeed *= hFriction;
			}
			rock.frameAngle -= xSpeed;
				if (collide("solid", x + FP.sign(xSpeed), y)) {
					rebound(x + FP.sign(xSpeed), y);
				}
				if (collide("solid", x, y + FP.sign(ySpeed))) {
					rebound(x, y + FP.sign(ySpeed));
				}
			super.update();
		}

		public function destroy() : void {
			// Here we could place specific destroy-behavior for the Bullet.
			FP.world.remove(this);
		}

		public function rebound(x : Number, y : Number) : void {
			var hyp : Number = Math.sqrt(Math.pow(xSpeed, 2) + Math.pow(ySpeed, 2));

			var xNewSpeed : Number = x - this.x;
			var yNewSpeed : Number = y - this.y;

			var tempHyp : Number = Math.sqrt(Math.pow(xNewSpeed, 2) + Math.pow(yNewSpeed, 2));

			xNewSpeed /= tempHyp;
			yNewSpeed /= tempHyp;

			xSpeed = -xNewSpeed * hyp;
			ySpeed = -yNewSpeed * hyp;
		}

		public function setSpeed(x : Number, y : Number) : void {
			this.xSpeed = x;
			this.ySpeed = y;
		}
	}
}
